package cn.org.timespace.ons.util.web;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Security {

	public static String hasString(byte[] b) {
		String has = "", tmp = "";
		for (int i = 0; i < b.length; i++) {
			tmp = (java.lang.Integer.toHexString(b[i] & 0XFF));
			if (tmp.length() == 1)
				has = has + "0" + tmp;
			else
				has = has + tmp;
			if (i < b.length - 1)
				has = has + ":";
		}
		return has.toUpperCase();
	}

	// 加密
	public static String encrypt(String plainText) {
		MessageDigest md;
		byte[] b = null;
		
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			b = md.digest();
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return hasString(b);
	}

	// 验证
	public static boolean verify(String plainText, String encryptStr){
		return (encrypt(plainText).equals(encryptStr));
	}
}
